/*
 * AlwaysThread.java
 * 
 * last update: 15.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 *
 * Obs: N/A
 */

package engine;

/**
 * It executes his instructions over and over..
 */
class AlwaysThread extends ControlThread{


	AlwaysThread (Instruction ins){
		super(ins);
		//we'll set alwayses to the highest priority, to allow them to  
		//perform all the monitoring stuff without losing information
		//setPriority(getThreadGroup().getMaxPriority()); 
		//not necessary anymore...
	}

	public void run(){
		try{
			try{
				Instruction cr = ins;
				while(true){  //THIS is where an always thread differs from a
					//ControlThread
					synchronized (xConsole.lock) {
						if (!xConsole.enableExecution()) {
							Time.stopSimulation();
							throw new SimulationStoppedException();
						}
						xConsole.consumeStep();
					}
					cr.execute();
					cr = cr.next();
					if(cr == null) cr = ins;
				}
				//xConsole.debug("always ended");
			}catch(InterpretTimeException ex){
				xConsole.dumpStack(ex);
				xConsole.cout(ex.toString());
				Time.stopSimulation();
			}
			ins = null;
			Time.removeThread(this);
		}catch(SimulationStoppedException ex){
			xConsole.debug(ex.toString() + "( alwaysthread) " +this);
		}
	}
}

